<template>
  <div class="enterprise-container">

    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" clearable placeholder="请输入楼宇名称" class="search-main" @clear="doSearch" />
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>

    <div class="create-container">
      <el-button type="primary" @click="add">添加楼宇</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="getList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="楼宇名称" width="320" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积(m²)" prop="area" />
        <el-table-column label="物业费(元/m²)" prop="propertyFeePrice" />
        <el-table-column label="状态" prop="status" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
    </div>

    <!-- 弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="isShow"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="louyuForm" :model="louyuForm" :rules="louyuRules" label-position="top" label-width="80px">
        <el-form-item label="楼宇名称" prop="name">
          <el-input v-model="louyuForm.name" />
        </el-form-item>
        <el-form-item label="楼宇层数" prop="floors">
          <el-input v-model="louyuForm.floors" />
        </el-form-item>
        <el-form-item label="在管面积" prop="area">
          <el-input v-model="louyuForm.area" />
        </el-form-item>
        <el-form-item label="物业费" prop="propertyFeePrice">
          <el-input v-model="louyuForm.propertyFeePrice" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="btn">确认</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { getListAPI, getAddAPI, getEditAPI, getLouyuEditAPI, getDelAPI } from '@/api/louyu'
export default {
  name: 'LouYu',
  data() {
    return {
      params: {
        name: '', // 楼宇名称
        page: 1, // 页数
        pageSize: 5 // 一页几条
      },
      total: 0,
      getList: [],
      isShow: false,
      louyuForm: {
        name: '', // 楼宇名称
        floors: '', // 层数
        area: '', // 在管面积
        propertyFeePrice: '' // 物业费
      },
      louyuRules: {
        name: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' }],
        floors: [{ required: true, message: '请输入层数', trigger: 'blur' }],
        area: [{ required: true, message: '请输入在管面积', trigger: 'blur' }],
        propertyFeePrice: [{ required: true, message: '请输入物业费', trigger: 'blur' }]
      },
      title: ''
    }
  },
  created() {
    this.params.page = 1
    this.getLouyuList()
  },
  methods: {
    // 渲染
    async getLouyuList() {
      const res = await getListAPI(this.params)
      console.log(res)
      this.getList = res.data.rows
      this.total = res.data.total
    },
    // 搜索
    doSearch() {
      this.params.page = 1
      this.getLouyuList()
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.getLouyuList()
    },
    // 点击新增
    add() {
      this.isShow = true
      this.title = '添加楼宇'
    },
    // 点击确认
    btn() {
      this.$refs.louyuForm.validate(async(isOk) => {
        if (isOk) {
          // 新增
          if (this.title === '添加楼宇') {
            await getAddAPI(this.louyuForm)
            this.$message.success('添加成功')
          }
          // 编辑
          if (this.title === '编辑楼宇') {
            const res = await getLouyuEditAPI(this.louyuForm)
            console.log('我是编辑确定', res)
            this.$message.success('编辑成功')
          }
          this.getLouyuList()
          this.isShow = false
          this.$refs.louyuForm.resetFields()
          this.params.page = 1
        }
      })
    },
    handleClose() {
      this.close()
    },
    // 点击取消
    close() {
      this.isShow = false
      this.$refs.louyuForm.resetFields()
    },
    // 点击编辑
    async edit(id) {
      this.isShow = true
      this.title = '编辑楼宇'
      this.louyuForm.id = id
      const res = await getEditAPI(id)
      console.log(res)
      this.louyuForm.name = res.data.name
      this.louyuForm.floors = res.data.floors
      this.louyuForm.area = res.data.area
      this.louyuForm.propertyFeePrice = res.data.propertyFeePrice
    },
    // 点击删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await getDelAPI(id)
        this.$message.success('删除成功')
        this.getLouyuList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
